package com.chenwy.example.spider;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

public class GithubPageProcessor implements PageProcessor {

    private Site site = Site.me().setRetryTimes(3).setSleepTime(100);
    @Override
    public void process(Page page) {
        page.addTargetRequests(page.getHtml().links().regex("(https://www.github.com\\.com/\\w+/\\w+)").all());
        page.putField("author", page.getUrl().regex("https://www.github\\.com/(\\w+)/.*").toString());
        page.putField("name", page.getHtml().xpath("//h1[@class='entry-title public']/strong/a/text()").toString());
        if (page.getResultItems().get("name")==null){
            //skip this page
            page.setSkip(true);
        }
        page.putField("readme", page.getHtml().xpath("//div[@id='readme']/tidyText()"));
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        /**
         * 显示https的握手过程，
         */
//        System.setProperty("javax.net.debug", "all");
//        System.setProperty("https.protocols", "TLSv1.3");
//        System.setProperty("jdk.tls.client.protocols", "TLSv1.3");
        Spider.create(new GithubPageProcessor()).addUrl("https://www.github.com").thread(5).run();
    }
}
